﻿using System;

namespace EventAggregator
{
    public sealed class EventMessageHandler : IDisposable
    {
        private readonly EventAggregator _eventAggregator;
        private readonly Action<EventMessage> _handler;
        private readonly int _id;

        public EventMessageHandler(int id, Action<EventMessage> handler, EventAggregator eventAggregator)
        {
            _id = id;
            _handler = handler;
            _eventAggregator = eventAggregator;
        }

        public int Id
        {
            get { return _id; }
        }

        #region IDisposable Members

        public void Dispose()
        {
            _eventAggregator.Unsubscribe(this);
        }

        #endregion

        public void HandleEvent(EventMessage eventMessage)
        {
            _handler(eventMessage);
        }
    }
}